﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace AgvPublic
{
    public class TcpClientPlc
    {
        protected TcpClient tcpClientPlc;
        public const int serverPort = 8501;//服务器端口
        public IPAddress ipTcp;//服务器IP
        public int clientPort;//本机客户端端口


        public TcpClientPlc(string serverIp,int port)
        {
            clientPort = port;
            IPEndPoint localRemoteEP = new IPEndPoint(IPAddress.Any, port);        
            tcpClientPlc = new TcpClient(localRemoteEP);
                   
            ipTcp = IPAddress.Parse(serverIp);           
            ConnectServer(serverIp, serverPort);
        }

        public void ConnectServer(string ip, int port)
        {
            try
            {                
                IPAddress ipAddress = IPAddress.Parse(ip);
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, serverPort);
                //connectDone.Reset();
                tcpClientPlc.BeginConnect(ipAddress, serverPort, new AsyncCallback(ConnectCallback), tcpClientPlc);
                //tcpClientPlc.Connect(remoteEP);
            }
            catch (Exception ex)
            {
                    
            }                   
        }


        private void ConnectCallback(IAsyncResult ar)
        {           
            TcpClient tc = (TcpClient)ar.AsyncState;
            try
            {
                tc.EndConnect(ar);             
            }
            catch (Exception ex)
            {
                //connectDone.Set();
            }
        }

        public bool SendData()
        {
            bool isConnect = false;
            try
            {               
                NetworkStream ntwStream = tcpClientPlc.GetStream();
                if (ntwStream.CanWrite)
                {
                    Byte[] bytSend = Encoding.UTF8.GetBytes("1");
                    ntwStream.Write(bytSend, 0, bytSend.Length);  
                    isConnect = true;
                }
                return isConnect;
            }
            catch
            {
                return false;
            }         
        }

        public void CloseTcpPlc()
        {            
            tcpClientPlc.Close();
        }


        public bool IsConnect()
        {
            if (tcpClientPlc.Connected)
            {
                return true;
            }
            else
                return false;
        }

        public string ReadDataFromPlc(string sendData)
        {
            try
            {
                NetworkStream dataSendStream = tcpClientPlc.GetStream();//定义网络流
                Byte[] sendByte = Encoding.ASCII.GetBytes(sendData);//把发送数据转换为ASCII数组          
                dataSendStream.Write(sendByte, 0, sendByte.Length); //发送通讯数据
                byte[] data = new byte[20];//设定接收数据为16位数组，接收数据不足用0填充
                dataSendStream.Read(data, 0, data.Length);


                //读取返回数据
                string readByte = "未返回数据";
                readByte = Encoding.ASCII.GetString(data);//接收数据从ASCII数组转换为字符串
                return readByte;          
            }
            catch(Exception ex)
            {
                return null;
            }
           
        }   
    }
}
